//这是单击字符弹出input框的函数,可以作为模板使用
var bangding = function() {
	var effectElement = $('.show_and_hid');
	inputs = effectElement.children('input');
	inputs.each(function() {
		self = this;
		if ($(self).val().length > 0) {
			$(self).css('display', 'none');
		}
		var spanBro = $(this).siblings('div');
		spanBro.click(function() {
			$(this).css('display', 'none');
			var myInput = $(this).siblings('input');
			myInput.css('display', 'inline');
			myInput.focus();
		});
		$(self).blur(function() {
			if ($(this).val().length > 0) {
				$(this).css('display', 'none');
			}
			$(this).siblings('div').css('display', 'inline-block');
		});
	});
};

var blankOrder = {
	'address': '',
	'custName': '',
	'id': '',
	'orderCancled': '',
	'orderDishes': [],
	'orderEnabled': '',
	'phone': '',
	'sum': '',
	'time': '',
	'orderStatus': '',
	'user': {
		'id': ''
	}
};

$(document).ready(function() {
	$(function() {
		$( "#order-tabs" ).tabs({
			active: 0,
			beforeLoad: function( event, ui ) {
				ui.jqXHR.error(function() {
					ui.panel.html("该页面不存在");
				});
			}
		});
	});
	
	var orderModel = function(orderModel) {
		var self = this;
		self.order = ko.mapping.fromJS(orderModel);
		self.orderDate = ko.computed(function() {
			if (self.order.time() == '') {
				return '';
			} else {
				return new Date(self.order.time()).format('yyyy-MM-dd      hh:mm:ss');
			}
		}, this);

		//计算所有菜式总价的绑定
		self.total = ko.computed(function() {
			var total = 0;
			var totalOrderDishes = self.order.orderDishes();
			var orderDishesLength = totalOrderDishes.length;
			for (var i = 0; i < orderDishesLength;i++) {
				var orderDish = totalOrderDishes[i];
				if (orderDish.dishEnabled()) {
					var sum = orderDish.num() * orderDish.price();
					sum = roundNumber(sum, 2);
					orderDish.dishSum(sum);
					total = total + parseFloat(orderDish.dishSum());
				}
			}
			return roundNumber(total, 2);
		}, self);
		
		self.orderStatus = ko.computed(function() {
			if (self.order && self.order.id() > 0) {
				return '正在编辑订单';
			} else {
				return '暂时没有需要处理的订单';
			}
		}, self);

		self.orderId = ko.observable('');
		self.getOrderById = function() {
			var selfId = self.orderId();
			if (selfId && selfId.Trim() != '') {
				$.get('order/' + selfId, function(response) {
					if (response.orderCancled == true) {
						self.orderId('');
						alert('订单已经被取消');
					} else if(response.orderEnabled == true) {
						self.orderId('');
						alert('订单已经生效');
					} else {
						ko.mapping.fromJS(response.order, self.order);
						bangding();
					}
				});
			}
		};
		
		self.cancledOrder = function() {
			$.get('cancled/' + self.order.id() + '?_method=PUT', function(response) {
				if (response.success == true) {
					self.getAutoOrder();
					alert('删除成功');
				} else {
					alert('删除失败');
				}
			});
		};
		
		self.enabledOrder = function() {
			$.ajax({
				type: 'post',
				url: 'enabled?_method=PUT',
				contentType: "application/json",//application/xml
				processData: true,//contentType为xml时，些值为false
				dataType: "json",//json--返回json数据类型；xml--返回xml
				data: JSON.stringify(ko.mapping.toJS(self.order)),
				success: function(response) {
					self.getAutoOrder();
				},            
				error:function(e){
				}
			});
		};
		
		self.getAutoOrder = function() {
			$.ajax({
				type : 'get',
				url : 'currentorder',
				data : '',
				async : true,
				success : function(response){
					if (response.success == true) {
						//console.log(response.order);
						ko.mapping.fromJS(response.order, self.order);
						bangding();
					} else {
						ko.mapping.fromJS(blankOrder, self.order);
						$('#orderDishTable input').hide();
					}
				}
			});
		};
	};

	var enabledModel = new orderModel(blankOrder);
	ko.applyBindings(enabledModel, document.getElementById('tabs-enabled'));
	$('#orderDishTable input').hide();
});